Skip to content

Fix MCP HTTP server resource leaks and error handling gaps#17

Merged
mars167 merged 8 commits intomainfrom
copilot/fix-mcp-http-connection-issues
Feb 4, 2026
Merged

Fix MCP HTTP server resource leaks and error handling gaps#17
mars167 merged 8 commits intomainfrom
copilot/fix-mcp-http-connection-issues

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 3, 2026

Addressed code review feedback on MCP HTTP server implementation. Fixed resource leaks, race conditions, and missing error boundaries.

Resource Management

  • Signal handlers: Changed process.on()process.once() to prevent duplicate registration on multiple startHttp() calls
  • Shutdown cleanup: Call transport.close() instead of invoking onclose callback directly; wait for all transports to close
  • Connection failures: Added transport.close() in error paths for both stateless and session modes
// Before: callback invocation doesn't actually close transport
for (const [sessionId, session] of sessions.entries()) {
  if (session.transport.onclose) {
    session.transport.onclose();
  }
}

// After: proper async cleanup with Promise.allSettled
const closePromises = [];
for (const [sessionId, session] of sessions.entries()) {
  closePromises.push(session.transport.close());
}
await Promise.allSettled(closePromises);

Error Handling

  • URL parsing: Wrapped in try-catch, returns 400 on malformed URLs
  • Session race condition: Added null check after Map.get(), returns 410 if session deleted between has() and get()
  • Double-delete protection: Guard onclose callback with sessions.has() check

Code Quality

  • Removed dead setupServerHandlers() wrapper method
  • Removed redundant String() type conversion

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 4 commits February 3, 2026 06:41
Co-authored-by: mars167 <29228178+mars167@users.noreply.github.com>
- Add comprehensive error handling for HTTP requests and server connections
- Fix session management to return session ID in response headers
- Eliminate code duplication by consolidating handler setup into attachServerHandlers
- Add graceful shutdown mechanism with SIGTERM/SIGINT handlers
- Store both transport and server instances in session map
- Add proper error handling for stateless mode
- Fix race condition in session initialization

Co-authored-by: mars167 <29228178+mars167@users.noreply.github.com>
- Create English MCP documentation (docs/mcp.md)
- Update Chinese MCP documentation with HTTP improvements
- Document session management and persistence
- Document error handling and graceful shutdown
- Update README.md and README.zh-CN.md with HTTP features
- Add session usage examples

Co-authored-by: mars167 <29228178+mars167@users.noreply.github.com>
Remove redundant 'as never' cast from Promise<never> return statement

Co-authored-by: mars167 <29228178+mars167@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix issues in MCP HTTP connection implementation Fix MCP HTTP server connection handling and session management Feb 3, 2026
Copilot AI requested a review from mars167 February 3, 2026 06:50
@mars167 mars167 marked this pull request as ready for review February 3, 2026 07:28
…nagement

- Change signal handlers from on() to once() to prevent duplicates
- Remove redundant setupServerHandlers() method (dead code)
- Remove redundant String() type conversion
- Add URL parsing error handling with 400 status
- Add session race condition check with 410 status
- Add proper transport cleanup on connection failures
- Improve shutdown to properly close transports instead of just invoking callbacks
- Add defensive check in onclose callback to prevent double-delete

Co-authored-by: mars167 <29228178+mars167@users.noreply.github.com>
Copilot AI changed the title Fix MCP HTTP server connection handling and session management Fix MCP HTTP server resource leaks and error handling gaps Feb 3, 2026
- Add stripLanguageKeywords() to symbolSearch.ts to remove language keywords (class, function, etc.) from search queries
- Apply keyword stripping in filterAndRankSymbolRows before symbol matching
- Add comprehensive diagnostics to handleRepoMap with detailed logging
- Improve error messages for empty repo_map results with actionable suggestions
- Enhance debugging capabilities for both handlers

These changes improve MCP tool usability by:
1. Allowing natural language queries like 'class Project' instead of just 'Project'
2. Providing clear diagnostic information when searches return no results
3. Better error handling and user guidance for index-related issues
The package-lock.json was out of sync with package.json, causing
CI to fail with 'Missing: hono@4.11.7 from lock file' error.

Regenerated lock file by running:
- rm -f package-lock.json
- npm install

Build and all tests verified passing locally.
@mars167 mars167 merged commit 6abfdd6 into main Feb 4, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants